import { Component, OnInit, Input } from "@angular/core";
declare let echarts: any;

@Component({
  selector: "app-customer-source",
  templateUrl: "./customer-source.component.html",
  styleUrls: ["./customer-source.component.scss"]
})
export class CustomerSourceComponent implements OnInit {
  @Input() tagData: any;

  chartOption: any; // 条形图数据
  appTypeCustom: any;
  constructor() {
  }

  ngOnInit() {
    const bodyStyles = window.getComputedStyle(document.body);
    const echart = echarts.init(document.getElementById("CustomerSource"));
    this.appTypeCustom = bodyStyles.getPropertyValue('--appTypeCustom').trim().replace('"', '').replace('"', '');
    const timer = setTimeout(() => {
      this.chartOption = {
        grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: 0
        },
        xAxis: {
          type: "value",
          show: false,
          max: this.tagData.data.data[0].maxValue
          // boundaryGap: [0, 0]
        },
        yAxis: {
          type: "category",
          show: false
        },
        series: [
          {
            type: "bar",
            barCategoryGap: 90,
            z: 0,
            barWidth: 8,
            barGap: "-100%",
            data: this.tagData.data.data.map(v => v.maxValue),
            itemStyle: {
              normal: {
                color: this.appTypeCustom !== 'LJ' ? "#FFEFE3" : "#FFF8E6",
                barBorderRadius: 8
              }
            },
            label: {
              normal: {
                show: true,
                align: "right",
                textStyle: {
                  color: this.appTypeCustom !== 'LJ' ? "#27282E" : "#252629",
                  fontSize: 16,
                  // fontFamily: "PingFangSC-Semibold,PingFangSC",字体去掉
                  fontWeight: 600
                },
                position: ["98%", -25],
                formatter: data => {
                  return this.tagData.data.data[data.dataIndex].numLabel;
                }
              }
            }
          },
          {
            type: "bar",
            barCategoryGap: 90,
            z: 1,
            barWidth: 8,
            data: this.tagData.data.data,
            itemStyle: {
              normal: {
                color: params => {
                  return {
                    x: 0,
                    y: 0,
                    x2: 1,
                    y2: 0,
                    colorStops: [
                      {
                        offset: 0,
                        color: this.appTypeCustom !== 'LJ' ? "#FF9500" : "#FFE14A"
                      },
                      {
                        offset: 1,
                        color: this.appTypeCustom !== 'LJ' ? "#FF6314" : "#FFCA3A"
                      }
                    ]
                  };
                },
                barBorderRadius: 8
              }
            },
            label: {
              normal: {
                show: true,
                textStyle: {
                  color: this.appTypeCustom !== 'LJ' ? "#27282E" : "#252629",
                  fontSize: 16
                },
                position: [0, -25],
                formatter: data => {
                  let icon = "";
                  switch (data.data.icon) {
                    case "build":
                      icon = "{build| }" + "  ";
                      break;
                    case "school":
                      icon = "{school| }" + "  ";
                      break;
                    case "hospital":
                      icon = "{hospital| }" + "  ";
                      break;
                    case "metro":
                      icon = "{metro| }" + "  ";
                      break;
                    case "transit":
                      icon = "{transit| }" + "  ";
                      break;
                    case "village":
                      icon = "{village| }" + "  ";
                      break;
                    case "market":
                      icon = "{market| }" + "  ";
                      break;
                    default:
                      icon = "";
                      break;
                  }
                  return icon + data.data.title;
                },
                rich: {
                  build: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_build@2x.png"
                    }
                  },
                  school: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_school@2x.png"
                    }
                  },
                  hospital: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_hospital@2x.png"
                    }
                  },
                  metro: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_metro@2x.png"
                    }
                  },
                  transit: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_transit@2x.png"
                    }
                  },
                  village: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_village@2x.png"
                    }
                  },
                  market: {
                    height: 16,
                    width: 16,
                    backgroundColor: {
                      image: "../../../assets/icon/details_icon_market@2x.png"
                    }
                  }
                }
              }
            }
          }
        ]
      };
      echart.setOption(this.chartOption);
      clearTimeout(timer);
    }, 100);
  }

}
